package cn.ncupyh.soft.staticInnerClass.students.util;

import cn.ncupyh.soft.students.entity.HomeworkRequirement;
import cn.ncupyh.soft.students.entity.HomeworkResult;
import cn.ncupyh.soft.students.entity.Teacher;

import java.util.ArrayList;
import java.util.Scanner;

public class TeacherUtil {
//    a.发布作业信息
//    b.批改作业
    public void login(ArrayList<HomeworkRequirement> hwRequirement, ArrayList<HomeworkResult> hwResult
            ,ArrayList<Teacher> teachers){
        //输入教师的信息
        Teacher tea = new Teacher();
        //首页
        //创建集合
        final String releaseTask = "1";
        final String hwCorrecting = "2";
        final String forgetPassword = "3";
        final String exit = "0";
        loop:while (true){
            //目录
            System.out.println("---------作业管理系统---------");
            System.out.println("---------教师---登录---------");
            System.out.println("---1>发布作业信息  2>批改作业--");
            System.out.println("---3>忘记密码      0>退出-----");
            System.out.println("----------------------------");
            System.out.println("请输入选项:>");
            Scanner in = new Scanner(System.in);
            String input = in.next();
            //功能实现
            switch(input){
                case exit:{
                    System.out.println("退出!");
                    break loop;
                }
                case releaseTask :{//发布作业信息
                    releaseTask(hwRequirement, tea);
                    break ;
                }
                case hwCorrecting:{//批改作业
                    homeworkCorrecting(hwResult,tea);
                    break ;
                }
                case forgetPassword :{//忘记密码
                    forgetPassword(teachers);
                    break ;
                }
                default:{
                    System.out.println(" 输入错误,请重新输入!");
                    break ;
                }
            }
            }
        }


    //    b.批改作业
    public void homeworkCorrecting( ArrayList<HomeworkResult> hwResult,Teacher t){
        Scanner in = new Scanner(System.in);
        //显示作业提交信息--学号
        System.out.println("提交作业名单:");
        System.out.println("序号\t学号"	);
        for (int i = 0; i < hwResult.size(); i++) {
            System.out.println( (i+1) + "\t" + hwResult.get(i).getuID());
        }
        //选择批改的学生序号
        System.out.println("请输入要批改的学生序号:>");
        int i = in.nextInt();
        HomeworkResult hwr = hwResult.get(i-1);
        //显示学生答案
        System.out.println("该学生的答案为:" + hwr.getResult());
        System.out.println("请输入该学生的成绩:>");
        double grade = in.nextDouble();
        hwr.setGrade(grade);
    }


    //    a.发布作业信息
    public void releaseTask(ArrayList<HomeworkRequirement> hwRequirement,Teacher t) {
        Scanner in = new Scanner(System.in);
        String YN = "Y";
        boolean b = true;
        if(YN == "Y")
            b = true;
        else if(YN == "N")
            b = false;
        else System.out.println("输入错误!请重新输入!");
        loop :while (true) {
            //查询作业实体中是否有这个科目
            int subjectIndex = findSameSubject(hwRequirement, t.getSubjects());
            System.out.println("请输入发布作业的信息:>");
            String taskMessage = in.next();
            if(subjectIndex == -1){
                //如果没有，则新建一个记录
                //添加科目、作业要求、截止日期
                hwRequirement.get(hwRequirement.size()).setSubject(t.getSubjects());
                hwRequirement.get(hwRequirement.size()).setRestrict(taskMessage);
            }else{
                //添加作业要求、截止日期
                hwRequirement.get(subjectIndex).setRestrict(taskMessage);
            }
            int i = findSameSubject(hwRequirement, t.getSubjects());
            System.out.println("请输入作业提交的截止日期:>");
            String deadline = in.next();
            hwRequirement.get(i).setDeadline(deadline);
            t:while (b) {
                System.out.println("是否继续发布作业?(Y/N)");
                YN = in.next();
                if(YN == "Y") {
                    b = true;
                    break t;
                } else if(YN == "N") {
                    b = false;
                    break loop;
                } else System.out.println("输入错误!请重新输入!");
            }
        }
    }

    //查询作业实体中是否有这个科目
    public int findSameSubject(ArrayList<HomeworkRequirement> hwRequirement,String subject) {
        for (int j = 0; j < hwRequirement.size(); j++) {
            if (hwRequirement.get(j).getSubject().equals(subject)){
                return j;
            }
        }
        return -1;
    }

    //忘记密码
    private static void forgetPassword(ArrayList<Teacher> teachers) {
        Scanner in = new Scanner(System.in);
        System.out.println("请输入用户的ID(账号):");
        String count = in.next();
        //若存在ID,则返回ID索引,否则-1;
        int indexCount = haveID(teachers, count);
        if (indexCount == -1) {
            System.out.println("查无此用户!未注册!");
            return;
        }
        //继续 找回密码
        System.out.println("请输入身份证:");
        String ID = in.next();
        if(teachers.get(indexCount).getID().equals(ID)){
            System.out.println("请输入手机号:");
            String phoneNum = in.next();
            if(teachers.get(indexCount).getPhoneNum().equals(phoneNum)){
                System.out.println("请输入修改后的密码:");
                String password1 = in.next();
                teachers.get(indexCount).setPassword(password1);
                System.out.println("修改成功!");
                return;
            }else{
                System.out.println("账号信息不匹配，修改失败。");
                return;
            }
        }else{
            System.out.println("账号信息不匹配，修改失败。");
            return;
        }
    }

    //索引学生集合里面是否有这个账号
    private static int haveID(ArrayList<Teacher> teachers,String ID) {
        for (int i = 0;i < teachers.size();i++) {
            Teacher u = teachers.get(i);
            String uID = u.getID();
            if(uID.equals(uID)){
                //相等返回索引
                return i;
            }
        }
        return -1;
    }
}
